package com.sscl.basesample.activities

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import com.sscl.baselibrary.activity.BasePermissionCheckActivity
import com.sscl.baselibrary.utils.DebugUtil
import com.sscl.baselibrary.utils.ToastUtil
import com.sscl.basesample.MainActivity
import androidx.core.net.toUri

class PermissionCheckActivity : BasePermissionCheckActivity() {

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * 静态声明
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    companion object {

        private val PERMISSIONS = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            arrayOf(
                Manifest.permission.READ_MEDIA_AUDIO,
                Manifest.permission.READ_MEDIA_IMAGES,
                Manifest.permission.READ_MEDIA_VIDEO
            )
        }else{
            arrayOf(
                Manifest.permission.READ_MEDIA_AUDIO,
                Manifest.permission.READ_MEDIA_IMAGES,
                Manifest.permission.READ_MEDIA_VIDEO,
                Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
            )
        }

        private const val REQUEST_CODE_FILE_MANAGE = 2
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 实现方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


    override fun needHideNavigationBar(): Boolean {
        return true
    }

    /**
     * 需要请求的权限
     */
    override fun permissionsNeedRequest(): Array<String> {
        return PERMISSIONS
    }

    /**
     * 权限请求成功
     */
    override fun permissionsRequested() {
        checkFileManagePermission()
    }

    /**
     * 有权限被永久拒绝
     * @param failedPermissions 被拒绝的权限
     * @return 是否跳转到系统权限设置界面
     */
    override fun permissionsAlwaysDenied(failedPermissions: Array<String>): Boolean {
        DebugUtil.warnOut(TAG, failedPermissions.contentToString())
        ToastUtil.toast(this, "权限被永久拒绝，请手动授予权限", 30000)
        toSettingActivity()
        return true
    }

    /**
     * 权限被用户拒绝
     * @param failedPermissions 被拒绝的权限
     * @return 是否继续进行权限请求
     */
    override fun permissionsDenied(failedPermissions: Array<String>): Boolean {
        return true
    }

    /**
     * 权限请求失败
     */
    override fun permissionRequestFailed() {

    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 重写方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        checkFileManagePermission()
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 私有方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private fun checkFileManagePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // 先判断有没有权限
            if (Environment.isExternalStorageManager()) {
                toNextActivity()
            } else {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.data = "package:$packageName".toUri()
                startActivityForResult(intent, REQUEST_CODE_FILE_MANAGE)
            }
        } else {
            toNextActivity()
        }
    }

    private fun toNextActivity() {
        //本界面仅用于防止程序黑白屏。想要更改本界面的黑白屏的背景，手动在res文件夹下新建一个xml文件夹，再新建一个files_path.xml。在其中配置即可
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }
}